package com.StarStudio.DAO;


import com.ruoyi.common.core.Entity.CouponsStock;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
@SuppressWarnings("all")
public interface CouponsStockDAO {

    List<CouponsStock> findCouponsStockList(CouponsStock couponsStock);

    @Insert("insert into coupons_item values (#{item_id},#{coupons_id},#{coupons_name},#{have_been_sold},#{sold_order_id},#{add_time},#{sold_time},#{qr_code},#{del_flag})")
    void addCouponsStock(CouponsStock couponsStock);

    void updateCouponsStock(CouponsStock couponsStock);

    @Select("select item_id from coupons_item where coupons_id = #{coupon_id} and have_been_sold = false")
    List<Long> findCouponsStockIds(long coupon_id);

    @Delete("delete from coupons_item where item_id = #{item_id}")
    void deleteCouponsStock(int item_id);

    @Select("select count(*) from coupons_item where del_flag = false")
    long findAllRecordNumber();

    @Select("select * from coupons_item where coupons_id = #{coupon_id} and have_been_sold = #{have_been_sold}")
    List<CouponsStock> findUnsoldItems(@Param("coupon_id") long coupon_id,@Param("have_been_sold") boolean have_been_sold);

    @Select("select * from goods_item where coupons_id = #{coupon_id} and have_been_sold = #{have_been_sold}")
    List<CouponsStock> findSoldItems(@Param("coupon_id") long coupon_id, @Param("have_been_sold") boolean have_been_sold);

    @Select("select * from coupons_item where sold_order_id = #{order_id}")
    List<CouponsStock> findByOrder_id(long order_id);

    @Select("select * from coupons_item where coupons_id = #{coupon_id}")
    CouponsStock findByCoupons_id(long coupon_id);

    @Select("select item_id from coupons_item where coupons_id = #{id} and have_been_sold = false")
    List<Long> findUnsoldItemIdsByCouponsId(long id);

    @Select("select item_id from coupons_item where coupons_id = #{id} and have_been_sold = true")
    List<Long> findSoldItemIdsByCouponsId(long id);

    @Update("update coupons_item set del_flag = true where item_id = #{coupons_item_id}")
    void updateDelFlag(Long coupons_item_id);

    @Delete("delete from coupons_item where item_id = #{unsold_coupons_item_id}")
    void realDelete(Long unsold_coupons_item_id);

    @Update("update coupons_item set have_been_sold = true, sold_order_id = #{order_id}, sold_time = now() where coupons_id = #{coupons_id} and have_been_sold = false limit 1")
    void updateCouponsStockByOrder(@Param("order_id") Long order_id,@Param("coupons_id") Long coupons_id);

    @Select("select * from coupons_item where coupons_id = #{coupons_id} and sold_order_id = #{order_id} limit 1")
    CouponsStock findCouponsStockByCouponsIdAndOrderId(@Param("coupons_id") Long coupons_id,@Param("order_id") Long order_id);

    @Update("update coupons_item set coupons_name = #{couponName} where coupons_id = #{couponId}")
    void updateCouponName(@Param("couponName") String couponName,@Param("couponId") Long couponId);
}
